package com.wt.hrentSystem.hrent.service;

import com.wt.hrentSystem.common.entity.Page;
import com.wt.hrentSystem.common.service.BaseService;
import com.wt.hrentSystem.hrent.config.PicType;
import com.wt.hrentSystem.hrent.dao.BikeResourceDao;
import com.wt.hrentSystem.hrent.entity.BikeResource;
import com.wt.hrentSystem.hrent.entity.Pic;
import com.wt.hrentSystem.hrent.entity.dto.TagDto;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service("BikeResourceService")
public class BikeResourceService extends BaseService<BikeResourceDao, BikeResource> {

    @Resource
    PicService picService;

    @Resource
    TagService tagService;

    @Resource
    BikeResourceDao bikeResourceDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public BikeResource get(String id) {
        BikeResource bikeResource = super.get(id);
        Pic pic = new Pic();
        pic.setBikeId(bikeResource.getId());
        bikeResource.setPicList(picService.findList(pic));
        bikeResource.setTagList(tagService.findByBikeId(bikeResource.getId()));
        return bikeResource;
    }

    /**
     * 查询多条数据
     * @param bikeResource
     * @return
     */
    public List<BikeResource> findList(BikeResource bikeResource){
        return super.findList(bikeResource);
    }

    /**
     * 分页查询多条数据
     * @param page
     * @param bikeResource
     * @return 消息集合
     */
    public Page<BikeResource> findPage(Page page, BikeResource bikeResource) {
        Page page1 = super.findPage(page, bikeResource);
        List<BikeResource> list = page1.getList();

        for (BikeResource resource : list) {

            //找出车辆的所有图片
            Pic pic = new Pic();
            pic.setBikeId(resource.getId());
            List<Pic> picList = picService.findList(pic);
            resource.setPicList(picList);
            //查找所有的标签

            List<TagDto> tagList = tagService.findByBikeId(resource.getId());
            resource.setTagList(tagList);

        }
        return page1;
    }

    /**
     * 微信小程序通过地址查询车源
     */
    public Page<BikeResource> findBikeByConditionPage(Page page, BikeResource bikeResource){
        if(bikeResource.getPage()!=null && bikeResource.getPage().getPageSize()>0){
            page.setPageSize(bikeResource.getPage().getPageSize());
        }
        bikeResource.setPage(page);
        List<BikeResource> list = this.dao.findBikeByConditionPage(bikeResource);
        for (BikeResource resource : list) {

            //找出车源的所有图片
            Pic pic = new Pic();
            pic.setBikeId(resource.getId());
            List<Pic> picList = picService.findList(pic);
            resource.setPicList(picList);
            //查找所有的标签

            List<TagDto> tagList = tagService.findByBikeId(resource.getId());
            resource.setTagList(tagList);

        }
        page.setList(list);
        return page;
    }


    /**
     * 删除数据
     * @param bikeResource
     */

    public void delete(BikeResource bikeResource) {
        super.delete(bikeResource);
    }


    /**
     * 更新或插入数据
     */
    @Transactional
    public void save(BikeResource bikeResource){
        //判断车辆id是否为空，如果为空则为新增，如果不为空则为保存
        if(StringUtils.isBlank(bikeResource.getId())){
            //生成流水号
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
            Date date = new Date();
            String format = simpleDateFormat.format(date);
            String uid = "HR"+format+System.currentTimeMillis();
//            bikeResource.preInsert();
            bikeResource.setId(uid);
            bikeResource.setCreateBy(bikeResource.getCurrentUser());
            bikeResource.setUpdateBy(bikeResource.getCurrentUser());
            bikeResource.setCreateDate(new Date());
            bikeResource.setUpdateDate(new Date());
            //新增的车辆将其状态设置为0表示审核中
            bikeResource.setState(0);
            //将删除标志设置为0
            bikeResource.setDelFlag(0);
            //先将车辆图片全部插入
            List<Pic> picList = bikeResource.getPicList();
            for (Pic pic : picList) {
                pic.setCurrentUser(bikeResource.getCurrentUser());
                pic.preInsert();
                //设置车辆信息
                pic.setBikeId(bikeResource.getId());
                pic.setState(1);
                pic.setType(PicType.BikeResource);
                picService.insert(pic);
            }
            //将标签信息全部插入
            List<TagDto> tagList = bikeResource.getTagList();
            for (TagDto tagDto : tagList) {
                tagDto.setCurrentUser(bikeResource.getCurrentUser());
                tagDto.preInsert();
                //设置标签信息
                tagDto.setBikeId(bikeResource.getId());
                tagService.addBikeTags(tagDto);
            }
            //保存车辆信息
            this.dao.insert(bikeResource);
        }else{
            //如果车辆id不为空
            bikeResource.preUpdate();
            //如果更改了图片将原本的车辆图片查询出来，删除后在添加新的图片
            if(bikeResource.getPicList()!=null&& bikeResource.getPicList().size()>0){
                Pic picFind = new Pic();
                picFind.setBikeId(bikeResource.getId());
                List<Pic> oldList = picService.findList(picFind);
                //将原本的图片列表删除
                for (Pic pic : oldList) {
                    picService.realDelete(pic.getId());
                }
                //再将车辆图片全部插入
                List<Pic> picList = bikeResource.getPicList();
                for (Pic pic : picList) {
                    pic.setCurrentUser(bikeResource.getCurrentUser());
                    pic.preInsert();
                    //设置车辆信息
                    pic.setBikeId(bikeResource.getId());
                    pic.setState(1);
                    pic.setType(PicType.BikeResource);
                    picService.insert(pic);
                }
            }
            //如果更改了标签，将原来的所有标签全部删除，再添加新的标签
            if(bikeResource.getTagList()!=null&& bikeResource.getTagList().size()>0){
                tagService.deleteByBikeId(bikeResource.getId());
                List<TagDto> tagList = bikeResource.getTagList();
                for (TagDto tagDto : tagList) {
                    tagDto.setCurrentUser(bikeResource.getCurrentUser());
                    tagDto.preInsert();
                    //设置标签信息
                    tagDto.setBikeId(bikeResource.getId());
                    tagService.addBikeTags(tagDto);
                }
            }
            //保存车辆信息
            this.dao.update(bikeResource);
        }
    }

    //根据角色查询车辆信息
    public Page<BikeResource> findByRole(Page<BikeResource> page, BikeResource bikeResource) {
        if(bikeResource.getPage()!=null && bikeResource.getPage().getPageSize()>0){
            page.setPageSize(bikeResource.getPage().getPageSize());
        }
        bikeResource.setPage(page);
        List<BikeResource> list = this.dao.findByRolePage(bikeResource);
        for (BikeResource resource : list) {

            //找出车辆的所有图片
            Pic pic = new Pic();
            pic.setBikeId(resource.getId());
            List<Pic> picList = picService.findList(pic);
            resource.setPicList(picList);
            //查找所有的标签

            List<TagDto> tagList = tagService.findByBikeId(resource.getId());
            resource.setTagList(tagList);

        }
        page.setList(list);
        return page;
    }

    //查询某个用户的车辆数量
    public Integer getCountByUserId(String userId) {
        return this.dao.getCountByUserId(userId);
    }

    //查询我看过的车辆
    public Page<BikeResource> findViewedBikePage(Page<BikeResource> page, BikeResource bikeResource) {
        if(bikeResource.getPage()!=null && bikeResource.getPage().getPageSize()>0){
            page.setPageSize(bikeResource.getPage().getPageSize());
        }
        bikeResource.setPage(page);
        List<BikeResource> list = this.dao.findViewedBikePage(bikeResource);
        for (BikeResource resource : list) {

            //找出车辆的所有图片
            Pic pic = new Pic();
            pic.setBikeId(resource.getId());
            List<Pic> picList = picService.findList(pic);
            resource.setPicList(picList);
            //查找所有的标签

            List<TagDto> tagList = tagService.findByBikeId(resource.getId());
            resource.setTagList(tagList);

        }
        page.setList(list);
        return page;
    }

    //查询我收藏的车辆
    public Page<BikeResource> findCollectionBikePage(Page<BikeResource> page, BikeResource bikeResource) {
        if(bikeResource.getPage()!=null && bikeResource.getPage().getPageSize()>0){
            page.setPageSize(bikeResource.getPage().getPageSize());
        }
        bikeResource.setPage(page);
        List<BikeResource> list = this.dao.findCollectionBikePage(bikeResource);
        for (BikeResource resource : list) {

            //找出车辆的所有图片
            Pic pic = new Pic();
            pic.setBikeId(resource.getId());
            List<Pic> picList = picService.findList(pic);
            resource.setPicList(picList);
            //查找所有的标签

            List<TagDto> tagList = tagService.findByBikeId(resource.getId());
            resource.setTagList(tagList);

        }
        page.setList(list);
        return page;
    }

    //查询我正在租的车辆
    public Page<BikeResource> findRentingBikePage(Page<BikeResource> page, BikeResource bikeResource) {
        if(bikeResource.getPage()!=null && bikeResource.getPage().getPageSize()>0){
            page.setPageSize(bikeResource.getPage().getPageSize());
        }
        bikeResource.setPage(page);
        List<BikeResource> list = this.dao.findRentingBikePage(bikeResource);
        for (BikeResource resource : list) {

            //找出车辆的所有图片
            Pic pic = new Pic();
            pic.setBikeId(resource.getId());
            List<Pic> picList = picService.findList(pic);
            resource.setPicList(picList);
            //查找所有的标签

            List<TagDto> tagList = tagService.findByBikeId(resource.getId());
            resource.setTagList(tagList);

        }
        page.setList(list);
        return page;
    }

    public Integer findScaleCount(BikeResource bikeResource){
        return this.dao.findScaleCount(bikeResource);
    }
}
